﻿using NUnit.Framework;
using System;
using System.Text;
using Rhino.Mocks;
using Rhino.Mocks.Constraints;
using Is = NUnit.Framework.Is;

namespace Xtee.Core.Test
{
    [TestFixture]
    public class MessagePathStructureTests
    {
        //stored_messages\2013\10\23\2013-10-23__09-43-58__397677@localhost.request.txt
        private IPreciceTimeOfDay _preciceTimeOfDay; 
        private MessagePathStructure _requestPathStructure; 
        private MessagePathStructure _responsePathStructure;
        private MessagePathStructure _responsePathStructureForClient;
        private MessagePathStructure _requestPathStructureForClient;

        [SetUp]
        public void Setup()
        {
            _preciceTimeOfDay = Rhino.Mocks.MockRepository.GenerateStub<IPreciceTimeOfDay>();
            _preciceTimeOfDay.Stub(x => x.Resolve()).Return(new DateTime(635181590620128473));//21-04-22__012847
            _responsePathStructure = new MessagePathStructure(@"c:\stored_messages", false, "127.0.0.1","38210272737", "colors.mix.v1", _preciceTimeOfDay);
            _requestPathStructure = new MessagePathStructure(@"c:\stored_messages", true, "127.0.0.1","38210272737", "colors.mix.v1", _preciceTimeOfDay);
            _responsePathStructureForClient = new MessagePathStructure(@"c:\stored_messages", false,null, "38210272737", "colors.mix.v1", _preciceTimeOfDay);
            _requestPathStructureForClient = new MessagePathStructure(@"c:\stored_messages", true,null, "38210272737", "colors.mix.v1", _preciceTimeOfDay);
        }
        [Test]
        public void ShouldCreateDefaultFileStamp()
        {  
            var requestfullPath = _requestPathStructure.FullPath;
            var resonseFullPath = _responsePathStructure.FullPath; //
            Console.WriteLine(requestfullPath);
            Assert.That(requestfullPath, Is.EqualTo(@"c:\stored_messages\colors.mix.v1\2013\10\23\38210272737\2013-10-23__21-04-22__012847@127.0.0.1.request.txt"));
            Assert.That(resonseFullPath, Is.EqualTo(@"c:\stored_messages\colors.mix.v1\2013\10\23\38210272737\2013-10-23__21-04-22__012847@127.0.0.1.response.txt"));
            Assert.That(_responsePathStructureForClient.FullPath, Is.EqualTo(@"c:\stored_messages\colors.mix.v1\2013\10\23\38210272737\2013-10-23__21-04-22__012847.response.txt"));
            Assert.That(_requestPathStructureForClient.FullPath, Is.EqualTo(@"c:\stored_messages\colors.mix.v1\2013\10\23\38210272737\2013-10-23__21-04-22__012847.request.txt"));
        }

        [Test]
        public void CanArrangeStructureByFormatForRequest()
        {
            _requestPathStructure.SetFormat(@"[personalcode]\[day].txt\[client]\[year]nice\[service]\[timeofday]__[ms]\@[month].[type]"); 
            Assert.That(_requestPathStructure.FullPath, Is.EqualTo(@"c:\stored_messages\38210272737\23.txt\127.0.0.1\2013nice\colors.mix.v1\21-04-22__012847\@10.request")); 
        }
        [Test]
        public void CanArrangeStructureByFormatForResponse()
        {
            _responsePathStructure.SetFormat(@"[year]\[month]-[day]\[personalcode] [service]nice\[year]-[month]-[day]__[timeofday]__[ms]-[client].[type].txt"); 
            Assert.That(_responsePathStructure.FullPath, Is.EqualTo(@"c:\stored_messages\2013\10-23\38210272737 colors.mix.v1nice\2013-10-23__21-04-22__012847-127.0.0.1.response.txt"));
        }

        [ExpectedException(ExpectedException = typeof(MessagePathStructureInvalid), ExpectedMessage = "[year][month][day][timeofday][ms] and [type] is required to specify in format to ensure uniquness!")]
        [Test]
        public void TimeStampAndTypeIsMinimimumIsMandatoryInFormatToEnsureUniqueNess()
        {
            _requestPathStructure.SetFormat("[year]");
            var path =_requestPathStructure.FullPath;
        }

        [Test]
        public void ShouldIgnoreClientKeyWordIfNotForServer()
        {
            _requestPathStructureForClient.SetFormat(@"[year]-[client][month]-[day]__[timeofday]__[ms].[type].txt");
            Assert.That(_requestPathStructureForClient.FullPath, Is.EqualTo("c:\\stored_messages\\2013-10-23__21-04-22__012847.request.txt"));
        }
        [Test]
        public void ShouldArrangeWithMinimumRequiredKeyWords()
        { 
            _requestPathStructure.SetFormat("[year]-[month]-[day]__[timeofday]__[ms].[type].txt");
            Assert.That(_requestPathStructure.FullPath, Is.EqualTo("c:\\stored_messages\\2013-10-23__21-04-22__012847.request.txt"));
            Console.WriteLine(_requestPathStructure.FullPath);
        }
    }
}
